package com.flin.icesilkworm.examination.course.service.impl;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.flin.icesilkworm.examination.bas.constant.ITheExamineeDutyConstant;
import com.flin.icesilkworm.examination.bas.constant.ITheExamineeGroupConstant;
import com.flin.icesilkworm.examination.bas.po.TheExamineeDutyDo;
import com.flin.icesilkworm.examination.bas.po.TheExamineeGroupDo;
import com.flin.icesilkworm.examination.course.constant.ICourseProjectsConstant;
import com.flin.icesilkworm.examination.course.service.ICourseProjectsService;
import com.flin.icesilkworm.examination.gen.course.service.GeneratedCourseProjectsService;
import com.flin.icesilkworm.framework.jdbc.objects.CollectionUtils;
import com.flin.icesilkworm.framework.jdbc.pojo.JavaNameMap;
import com.flin.icesilkworm.framework.ui.pojo.ResultDto;
import com.flin.icesilkworm.framework.ui.pojo.UiViewForm;
@Service("courseProjectsService")
@Transactional
public class CourseProjectsServiceImpl extends GeneratedCourseProjectsService implements ICourseProjectsService,ICourseProjectsConstant{

	public ResultDto<JavaNameMap<?>> pageForMap(UiViewForm form) {
		ResultDto<JavaNameMap<?>> rd =  super.pageForMap(form);
		
		if(rd.getTotal() > 0){
			Set<String> dutyIds = new HashSet<String>();
			Set<String> groupIds = new HashSet<String>();
			for(JavaNameMap<?> map : rd.getRows()){
				String dutyId = map.getString(DUTY_IDS);
				if(dutyId != null){
					for(String di : dutyId.replace("[", "").replace("]", "").split(",")){
						dutyIds.add(di);
					}
				}
				String groupId = map.getString(GROUP_IDS);
				if(groupId != null){
					for(String gi : groupId.replace("[", "").replace("]", "").split(",")){
						groupIds.add(gi);
					}
				}
			}
			Map<String, Object> map = CollectionUtils.toMap(
					"duty",dutyIds.size() > 0 ? TheExamineeDutyDo.create().where(t->
						t.andDutyIdIn(dutyIds.stream().map(Long::parseLong).collect(Collectors.toList()))
						 .setAttribute(ITheExamineeDutyConstant.DUTY_ID,ITheExamineeDutyConstant.DUTY_NAME)
					).queryForWhere():null,
					"group",groupIds.size() > 0 ? TheExamineeGroupDo.create().where(t->
						t.andGroupIdIn(groupIds.stream().map(Long::parseLong).collect(Collectors.toList()))
						.setAttribute(ITheExamineeGroupConstant.GROUP_ID,ITheExamineeGroupConstant.GROUP_NAME)
					).queryForWhere():null
			);
			rd.setExtendsMap(map);
		}
		return rd;
	}
//	@Transactional(readOnly=true)
//	public ResultDto<JavaNameMap<?>> pageForMap(UiViewForm form) {
//	
//		ResultDto<JavaNameMap<?>> dto = newResultDto();
//		
//		CourseProjectsExample example = new CourseProjectsExample();
//		
//		populate(example,form);
//		
//		example.setAttribute(SIMPLE_LIST);
//		
//		if(form.isQueryCount()) dto.setTotal(courseProjectsMapper.countBy(example));
//		else dto.setTotal(-1);
//		
//		if(dto.getTotal() != 0){
//		
//			RowBounds rowBounds = new RowBounds((form.getPage()  - 1) * form.getRow(),form.getRow());
//		
//			dto.setRows((List<JavaNameMap<?>>)courseProjectsMapper.listBy(example,rowBounds));
//			
//			dto.setPage(dto.getTotal() / form.getRow() + ((dto.getTotal() % form.getRow() == 0) ? 0 : 1));
//		
//		}
//		
//		
//		return dto;
//	}
//	
}